package com.yqyd.hy.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.yqyd.hy.domain.HyGoods;
import com.yqyd.hy.mapper.HyGoodsMapper;
import com.yqyd.hy.service.IHyGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 宏悦—商品Service业务层处理
 * 
 * @author Dhq
 * @date 2025-06-16
 */
@Service
public class HyGoodsServiceImpl implements IHyGoodsService
{
    @Autowired
    private HyGoodsMapper hyGoodsMapper;

    /**
     * 查询宏悦—商品
     * 
     * @param id 宏悦—商品主键
     * @return 宏悦—商品
     */
    @Override
    public HyGoods selectHyGoodsById(Long id)
    {
        return hyGoodsMapper.selectHyGoodsById(id);
    }

    /**
     * 查询宏悦—商品列表
     * 
     * @param hyGoods 宏悦—商品
     * @return 宏悦—商品
     */
    @Override
    public List<HyGoods> selectHyGoodsList(HyGoods hyGoods)
    {
        return hyGoodsMapper.selectHyGoodsList(hyGoods);
    }

    /**
     * 新增宏悦—商品
     * 
     * @param hyGoods 宏悦—商品
     * @return 结果
     */
    @Override
    public int insertHyGoods(HyGoods hyGoods)
    {
        return hyGoodsMapper.insertHyGoods(hyGoods);
    }

    /**
     * 修改宏悦—商品
     * 
     * @param hyGoods 宏悦—商品
     * @return 结果
     */
    @Override
    public int updateHyGoods(HyGoods hyGoods)
    {
        HyGoods oldHyGoods = hyGoodsMapper.selectHyGoodsById(hyGoods.getId());
        // 新闻价大于原价，更新历史价
        if(Objects.nonNull(oldHyGoods)
                && Objects.nonNull(hyGoods.getNewsPrice())
                && hyGoods.getNewsPrice().compareTo(oldHyGoods.getNewsPrice()) != 0 ){
            hyGoods.setHistoryPrice(oldHyGoods.getNewsPrice());
        }
        hyGoods.setUpdateAt(new Date());
        return hyGoodsMapper.updateHyGoods(hyGoods);
    }

    /**
     * 批量删除宏悦—商品
     * 
     * @param ids 需要删除的宏悦—商品主键
     * @return 结果
     */
    @Override
    public int deleteHyGoodsByIds(Long[] ids)
    {
        return hyGoodsMapper.deleteHyGoodsByIds(ids);
    }

    /**
     * 删除宏悦—商品信息
     * 
     * @param id 宏悦—商品主键
     * @return 结果
     */
    @Override
    public int deleteHyGoodsById(Long id)
    {
        return hyGoodsMapper.deleteHyGoodsById(id);
    }

    @Override
    public String UpdateTime(){
        return hyGoodsMapper.UpdateTime();
    }
}
